package cn.ffcs.bpmpvc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.echoice.hibernate.HibernateEntityDao;
import org.echoice.paper.PageBean;
import org.springframework.jdbc.core.RowMapper;

import cn.ffcs.bpmpvc.dao.ProductFlowDao;
import cn.ffcs.bpmpvc.utils.FlowContextUtil;
import cn.ffcs.bpmpvc.web.views.ProductReferAttachView;
import cn.ffcs.common.model.Attach;
import cn.ffcs.product.model.Product;
import cn.ffcs.product.model.ProductReference;
import cn.ffcs.productdict.model.ProductInfo;
import cn.ffcs.ref.model.ProductRefSort;
import cn.ffcs.system.dict.model.Dict;

public class ProductFlowDaoImpl extends HibernateEntityDao<Product> implements ProductFlowDao {

	public PageBean searchProductPageList(Product product, int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		StringBuilder sql=new StringBuilder();
		sql.append("select t1.req_code,t1.name,t2.name personName,t1.begin,t1.procinst_id from t_busi_product t1,v_secu_user t2");
		sql.append(" where t1.creator=t2.user_id and t1.procinst_id is not null");
		List parmList=new ArrayList();
		if(StringUtils.isNotBlank(product.getName())){
			sql.append(" and t1.name like ?");
			parmList.add("%"+product.getName()+"%");
		}
		
		if(StringUtils.isNotBlank(product.getReqCode())){
			sql.append(" and t1.req_code=?");
			parmList.add(product.getReqCode());
		}
		
		if(StringUtils.isNotBlank(product.getCreator())){
			sql.append(" and t2.name=?");
			parmList.add(product.getCreator());
		}
		
		return super.pagedQuerySQL(sql.toString(), pageNo, pageSize, parmList.toArray());
	}

	public Product getByProinstId(String procInstId){
		return super.findUniqueBy("procinstId", procInstId);
	}
	
	public List<ProductRefSort> findProductRefSortList(String productId){
		String hql="select t from ProductRefSort t where t.product=?";
		return getHibernateTemplate().find(hql, productId);
	}

	public List<ProductInfo> findProductInfos(String productId){
		String sql="select t1.id,t1.product,t1.product_property,t2.product_property product_property_name,t1.property_value" +
				" from t_busi_product_info t1,t_busi_product_dict t2 where t1.product_property=t2.id and t1.product=?";
		List<ProductInfo> list=getJdbcTemplate().query(sql, new Object[]{productId}, new RowMapper(){

			public Object mapRow(ResultSet rs, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				ProductInfo obj=new ProductInfo();
				obj.setId(rs.getInt("id"));
				obj.setProduct(rs.getString("product"));
				obj.setProductProperty(rs.getString("product_property"));
				obj.setProductPropertyName(rs.getString("product_property_name"));
				obj.setPropertyValue(rs.getString("property_value"));
				return obj;
			}});
		return list;
	}
	
	public List<ProductReferAttachView> findProductReferAttachList(String productId){
		String sql="select t1.id,t1.name,t1.full_name,t1.save_type,t1.ref_entity_id,t2.reference_type,t3.name reference_type_name,t1.begin,t1.relative_path";
		sql+=" from t_comm_attach t1, t_busi_product_reference t2,t_sys_dict t3";
		sql+=" where t1.ref_entity_id = t2.id and t2.reference_type=t3.id";
		sql+=" and t1.ref_entity=? and t2.product=? and t1.end>sysdate order by t3.sorting asc";
		List<ProductReferAttachView> list=getJdbcTemplate().query(sql, new Object[]{ProductReference.class.getName(),productId}, new RowMapper(){

			public Object mapRow(ResultSet rs, int arg1) throws SQLException {
				// TODO Auto-generated method stub
				ProductReferAttachView obj=new ProductReferAttachView();
				Attach attach=new Attach();
				ProductReference productReference=new ProductReference();
				attach.setId(rs.getInt("id"));
				attach.setName(rs.getString("name"));
				attach.setRefEntityID(rs.getString("ref_entity_id"));
				attach.setFullName(rs.getString("full_name"));
				attach.setSaveType(rs.getInt("save_type"));
				productReference.setId(Integer.valueOf(attach.getRefEntityID()));
				
				Dict dict=new Dict();
				dict.setId(Integer.valueOf(rs.getString("reference_type")));
				dict.setName(rs.getString("reference_type_name"));
				productReference.setReferenceType(dict);
				
				obj.setAttach(attach);
				obj.setProductReference(productReference);
				return obj;
			}});
		return list;
	}
	
	public void updateProduct(String productId){
		String sql="update t_busi_product t set t.end=? where t.id=?";
		getJdbcTemplate().update(sql, new Object[]{FlowContextUtil.getEndDate(),productId});
	}
	
	public void updateProductRefer(String productId){
		String sql="update t_busi_product_reference t set t.end=? where t.product=?";
		getJdbcTemplate().update(sql, new Object[]{FlowContextUtil.getEndDate(),productId});
	}
}
